package org.msfj.toilets.data.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.msfj.toilets.data.mapper.PositionInfoMapper;
import org.msfj.toilets.data.service.PositionInfoService;
import org.msfj.toilets.model.PositionInfo;
import org.msfj.toilets.vo.PositionInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 系统蹲位信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "positionInfo")
public class PositionInfoServiceImpl extends BaseServiceImpl<PositionInfo> implements PositionInfoService {

    @Autowired
    PositionInfoMapper positionInfoMapper;

    /**
     * 根据条件分页查询系统蹲位信息列表
     *
     * @param positionInfoVo
     * @param page           当前第几页
     * @param rows           每页多少条记录
     * @return PageInfo<PositionInfo>
     */
    @Override
    public PageInfo<PositionInfoVo> getByPositionInfo(PositionInfoVo positionInfoVo, int page, int rows) {
        Example example = new Example(PositionInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        PageHelper.startPage(page, rows);
        List<PositionInfo> positionInfoList = super.getByExample(example);
        PageInfo positionInfoVoPageInfo = new PageInfo(positionInfoList);
        positionInfoVoPageInfo.setList(setOtherProperties(positionInfoList));
        return positionInfoVoPageInfo;
    }

    /**
     * 根据条件查询所有系统蹲位信息对象列表
     *
     * @param positionInfoVo
     * @return List<PositionInfo>
     */
    @Override
    public List<PositionInfoVo> getAllPositionInfo(PositionInfoVo positionInfoVo) {
        Example example = new Example(PositionInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        if (ObjectUtil.isNotNull(positionInfoVo.getCreateTime())) {
            String format = DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN);
            criteria.andBetween("create_time", DateUtil.parse(format + "00:00:00").toJdkDate(), DateUtil.parse(format + "23:59:59").toJdkDate());
        }
        example.setOrderByClause("sort_no asc");
        List<PositionInfoVo> positionInfoVoList = setOtherProperties(super.getByExample(example));
        return positionInfoVoList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param positionInfo
     * @return DepartmentVo
     */
    @Override
    public PositionInfoVo setOtherProperties(PositionInfo positionInfo) {
        PositionInfoVo positionInfoVo = new PositionInfoVo();
        BeanUtils.copyProperties(positionInfo, positionInfoVo);
        return positionInfoVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param positionInfos
     * @return List<PositionInfoVo>
     */
    @Override
    public List<PositionInfoVo> setOtherProperties(List<PositionInfo> positionInfos) {
        List<PositionInfoVo> positionInfoVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(positionInfos)) {
            for (PositionInfo positionInfo : positionInfos) {
                PositionInfoVo positionInfoVo = new PositionInfoVo();
                BeanUtils.copyProperties(positionInfo, positionInfoVo);
                positionInfoVos.add(positionInfoVo);
            }
        }
        return positionInfoVos;
    }
}
